/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker;

import log.Debug;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import extension.poker.zone.Flag;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author daz
 */
public class Response {

    PokerRoomExtension ext;

    public Response(PokerRoomExtension ext) {
        this.ext = ext;
    }

    public void sendPlayerRoles(int dealer, long smallBlind, long bigBlind, long amountBlindBet, long playerSmallBlindMoney, long playerBigBlindMoney, List<User> listUsers) {
        Debug.d("send player roles");
        ISFSObject resObj = new SFSObject();
        resObj.putInt("room_player_dealer", dealer);

        resObj.putLong("room_player_small_blind", smallBlind);
        resObj.putLong("room_player_big_blind", bigBlind);
        resObj.putLong("room_amount_blind", amountBlindBet);
        resObj.putLong("player_small_blind_money", playerSmallBlindMoney);
        resObj.putLong("player_big_blind_money", playerBigBlindMoney);

        ext.send(Command.PLAYER_ROLES, resObj, listUsers);
        log(resObj);


    }

    public void sendBettingTurn(int bettingTurn, int bettingOption, long raiseMin, long raiseMax, List<User> userList) {
        ISFSObject resObj = new SFSObject();
        resObj.putLong(PokerFlag.RAISE_MIN, raiseMin);
        resObj.putLong(PokerFlag.RAISE_MAX, raiseMax);
        resObj.putInt(PokerFlag.BETTING_TURN, bettingTurn);
        resObj.putInt(PokerFlag.BETTING_OPTION, bettingOption);//0: can check ,1:cant check
        ext.send(Command.BETTING_TURN, resObj, userList);
        Debug.d("send betting turn");
        Debug.d("raise min:" + raiseMin);
        Debug.d("raise max:" + raiseMax);
        Debug.d("bet turn:" + bettingTurn);
        Debug.d("bet option:" + bettingOption);
    }

    public void sendDealCard(int card1, int card2, User user) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.ROOM_DEAL_CARD1, card1);
        resObj.putInt(PokerFlag.ROOM_DEAL_CARD2, card2);
        ext.send(Command.DEAL_CARDS, resObj, user);
        Debug.d("send deal card");
        Debug.d("user " + user.getName());
        Debug.d("card 1:" + card1);
        Debug.d("card 2:" + card2);
    }

    public void sendBettingAction(int bettingChoice, long bettingMoney, int bettingPlayer, long playerMoney, List<User> listUsers) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.BETTING_CHOICE, bettingChoice);
        resObj.putLong(PokerFlag.BETTING_MONEY, bettingMoney);
        resObj.putInt(PokerFlag.PLAYER_POSITION, bettingPlayer);
        resObj.putLong(PokerFlag.PLAYER_MONEY, playerMoney);
        ext.send(Command.BETTING_ACTION, resObj, listUsers);
        log(resObj);
    }

    public void sendNewRoundInfo(int currentRound, List<Integer> communityCard, List<User> listUsers) {
        ISFSObject resObj = new SFSObject();
//        resObj.putInt(PokerFlag.POT_AMOUNT, potAmount);
        resObj.putInt(PokerFlag.POKER_ROUND, currentRound);
        resObj.putIntArray(PokerFlag.COMMUNITY_CARD, communityCard);
        ext.send(Command.NEW_ROUND, resObj, listUsers);
        Debug.d("send new round info");
//        Debug.d("pot amout:" + potAmount);
        Debug.d("round:" + PokerConversion.getRoundName(currentRound));
        Debug.d("card:" + communityCard.toString());
    }

    public void sendNewRoundInfo(int currentRound, int communityCard, List<User> listUsers) {
        ISFSObject resObj = new SFSObject();
//        resObj.putInt(PokerFlag.POT_AMOUNT, potAmount);
        resObj.putInt(PokerFlag.POKER_ROUND, currentRound);
        resObj.putInt(PokerFlag.COMMUNITY_CARD, communityCard);
        ext.send(Command.NEW_ROUND, resObj, listUsers);
        Debug.d("send new round info");
//        Debug.d("pot amount:" + potAmount);
        Debug.d("big blind:" + currentRound);
        Debug.d("card:" + communityCard);
    }

    public void sendMatchResult(int matchWinner, long potAmount,
            List<User> listUsers) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.MATCH_WINNER, matchWinner);
        resObj.putLong(PokerFlag.MATCH_MONEY_WIN, potAmount);
        ext.send(Command.MATCH_RESULT, resObj, listUsers);
        Debug.d("send match result");
        Debug.d("match winner:" + matchWinner);
        Debug.d("money win:" + potAmount);
    }

    public void sendJoinGameInfo(int chairId, String name, long moneyJoin, int result) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.PLAYER_JOIN_POS, chairId);
        resObj.putUtfString(PokerFlag.PLAYER_JOIN_NAME, name);
        resObj.putLong(PokerFlag.PLAYER_JOIN_MONEY, moneyJoin);
        resObj.putInt(PokerFlag.JOIN_GAME_RESULT, result);
        Room room = ext.getParentRoom();
        ext.send(Command.JOIN_GAME, resObj, room.getUserList());

        Debug.d("send join game info");
        Debug.d("chair id:" + chairId);
        Debug.d("name:" + name);
        Debug.d("money join :" + moneyJoin);

    }

    public void sendUserLeave(int leaveId, boolean isContinue) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt("player_leave_positions", leaveId);
        resObj.putInt("is_play", isContinue ? 1 : 0);
        Debug.d("send user leave");
        Debug.d("leave id:" + leaveId);
        ext.send(Command.PLAYER_LEAVE, resObj, ext.getParentRoom().getUserList());
    }

    public void sendRoomInfo(int isPlay, long potAmount, List<Integer> communityCard, int bettingTurn, long moneyJoinMin, long moneyJoinMax,
            String playerName, long playerMoney, int level, String levelName, User user) {
        ISFSObject resObj = new SFSObject();
        //room info
        resObj.putInt(PokerFlag.ROOM_IS_PLAY, isPlay);
        resObj.putLong(PokerFlag.ROOM_MONEY_BET, potAmount);
        resObj.putIntArray(PokerFlag.ROOM_COMMUNITY_CARD, communityCard);
        resObj.putInt(PokerFlag.ROOM_BETTING_TURN, bettingTurn);
        resObj.putLong(PokerFlag.ROOM_BET_MIN, moneyJoinMin);
        resObj.putLong(PokerFlag.ROOM_BET_MAX, moneyJoinMax);

        //player info
        resObj.putUtfString(PokerFlag.PLAYER_NAME, playerName);
        resObj.putLong(PokerFlag.PLAYER_MONEY, playerMoney);
        resObj.putInt(PokerFlag.PLAYER_LEVEL, level);
        resObj.putUtfString(PokerFlag.PLAYER_LEVEL_NAME, levelName);

        ext.send(Command.ROOM_INFO, resObj, user);

    }

    public void sendListPlayer(List<Integer> listPlayerPositions, List<String> listPlayerNames, List<Long> listPlayerMoneys, List<Long> listMoneyBet, User user) {
        ISFSObject resObj = new SFSObject();
        resObj.putIntArray(PokerFlag.ROOM_PLAYER_POS, listPlayerPositions);
        resObj.putUtfStringArray(PokerFlag.ROOM_PLAYER_NAME, listPlayerNames);
        resObj.putLongArray(PokerFlag.ROOM_PLAYER_MONEY, listPlayerMoneys);
        resObj.putLongArray(PokerFlag.ROOM_PLAYER_BET, listMoneyBet);
        ext.send(Command.LIST_PLAYERS, resObj, user);
        Debug.d("send list player");
        Debug.d("pos:" + listPlayerPositions.toString());
        Debug.d("name:" + listPlayerNames.toString());
        Debug.d("money:" + listPlayerMoneys.toString());
        Debug.d("bet:" + listMoneyBet.toString());
    }

    public void sendTableInfo(String roomName, List<Integer> listPlayerPositions,
            List<String> listPlayerNames, List<Long> listPlayerMoneys, List<Long> listMoneyBet, User user) {
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(PokerFlag.ROOM_NAME, roomName);
        resObj.putIntArray(PokerFlag.ROOM_PLAYER_POS, listPlayerPositions);
        resObj.putUtfStringArray(PokerFlag.ROOM_PLAYER_NAME, listPlayerNames);
        resObj.putLongArray(PokerFlag.ROOM_PLAYER_MONEY, listPlayerMoneys);
        resObj.putLongArray(PokerFlag.ROOM_PLAYER_BET, listMoneyBet);
        ext.send(Command.TABLE_INFO, resObj, user);
        Debug.d("send list player");
        Debug.d("pos:" + listPlayerPositions.toString());
        Debug.d("name:" + listPlayerNames.toString());
        Debug.d("money:" + listPlayerMoneys.toString());
        Debug.d("bet:" + listMoneyBet.toString());
    }

    void sendListOutOfMoney(List<Integer> listOut, boolean isContinue) {
        Debug.d("send list out of money");
        ISFSObject resObj = new SFSObject();
        resObj.putIntArray(PokerFlag.OUT_OF_MONEY, listOut);
        int isPlay = (isContinue ? 1 : 0);
        resObj.putInt(PokerFlag.IS_PLAY, isPlay);
        ext.send(Command.PLAYER_OUT, resObj, ext.getParentRoom().getUserList());
        Debug.d("list out :" + listOut.toString());
        Debug.d("is play :" + isPlay);
    }

    void sendListWaiting(List<Integer> listSend) {
        ISFSObject resObj = new SFSObject();
        resObj.putIntArray(PokerFlag.PLAYER_WAITING, listSend);
        ext.send(Command.PLAYER_WAITING, resObj, ext.getParentRoom().getUserList());
        Debug.d("send list waiting");
        Debug.d("waiting list :" + listSend.toString());
    }

    void sendCardHint(int[] cardHint, int strength, User user) {
        List<Integer> listPos = new LinkedList<Integer>();
        for (int i = 0; i < 7; i++) {
            listPos.add(cardHint[i]);
        }
        ISFSObject resObj = new SFSObject();
        resObj.putIntArray("card_hint", listPos);
        resObj.putInt("strength", strength);
        ext.send(Command.CARD_HINT, resObj, user);
        Debug.d("send card hint");
        Debug.d("card hint :" + listPos.toString());
        Debug.d("card strength :" + strength);

    }

    public void sendMatchResult(int matchWinner, List<Integer> cardWin, int handType, long potAmount,
            List<User> listUsers, boolean isFold, List<Integer> cardArr, int segment, int numberWinnes, long playerMoney) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.NUMBER_WINNER, numberWinnes);
        resObj.putInt(PokerFlag.MATCH_WINNER, matchWinner);
        resObj.putInt(PokerFlag.RESULT_SEGMENT, segment);
        resObj.putLong(PokerFlag.PLAYER_MONEY, playerMoney);
        if (!isFold) {
            resObj.putIntArray(PokerFlag.CARD_WIN, cardWin);
            resObj.putInt(PokerFlag.CARD_STRENGTH_WIN, handType);
            resObj.putIntArray(PokerFlag.CARD_ORDER, cardArr);
            Debug.d("card win 1:" + cardWin.get(0));
            Debug.d("card win 2:" + cardWin.get(1));
            Debug.d("card order:" + cardArr.toString());
        }
        resObj.putLong(PokerFlag.MATCH_MONEY_WIN, potAmount);
        ext.send(Command.MATCH_RESULT, resObj, listUsers);
        Debug.d("send match result");
        Debug.d("match winner:" + matchWinner);
        Debug.d("type:" + PokerConversion.getHandName(handType));
        Debug.d("money win:" + potAmount);
    }

    public void sendSitOutState(ISFSObject resObj, List<User> listUsers) {
        ext.send(Command.SIT_OUT, resObj, listUsers);
    }

    public void sendStandUp(ISFSObject resObj, long betMoney, long playerMoney, List<User> listUsers) {
        resObj.putLong(PokerFlag.PLAYER_MONEY, playerMoney);
        resObj.putLong(PokerFlag.BETTING_MONEY, betMoney);
        ext.send(Command.STAND_UP, resObj, listUsers);
    }

    public void sendPotAmount(int potAmount, List<User> listUsers) {
        ISFSObject resObj = new SFSObject();
        resObj.putInt(PokerFlag.POT_AMOUNT, potAmount);
        ext.send(Command.POT_AMOUNT, resObj, listUsers);
        Debug.i("send pot amount");
        Debug.d("pot amount :" + potAmount);
    }

    public void sendMessageChat(String message, int type) {
        ISFSObject resObj = new SFSObject();
//        Date date = new Date();
//        String time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
        resObj.putUtfString(PokerFlag.CHAT_MESSAGE, message);
//        resObj.putUtfString(PokerFlag.CHAT_TIME, time);
//         resObj.putInt(PokerFlag.CHAT_TIME, type);
        Room room = ext.getParentRoom();
        ext.send(Command.ROOM_CHAT, resObj, room.getUserList());
//        Debug.line("send message");
//        Debug.d(message);
    }

    public void sendTableInfo(String roomName, List<String> listNames, List<Integer> listPos, User user) {
        Debug.d("send table info");
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.ROOM_NAME, roomName);
        if (listNames != null) {
            resObj.putUtfStringArray(PokerFlag.ROOM_PLAYER_NAME, listNames);
            resObj.putIntArray(PokerFlag.ROOM_PLAYER_POS, listPos);
            ext.send(Command.TABLE_INFO, resObj, user);
        }
    }

    public void sendTableInfo(String roomName, List<String> listNames, List<Integer> listPos, List<User> user) {
        Debug.d("send table info");
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.ROOM_NAME, roomName);
        if (listNames != null) {
            resObj.putUtfStringArray(PokerFlag.ROOM_PLAYER_NAME, listNames);
            resObj.putIntArray(PokerFlag.ROOM_PLAYER_POS, listPos);
            ext.send(Command.TABLE_PLAYERS, resObj, user);
        }
        log(resObj);
    }

    void sendListTable(List<Integer> numberPlayerInRooms, List<String> listRoomNames, String groupName, User user) {
        Debug.d("send list table to 1 user");
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.GROUP_NAME, groupName);
//        resObj.putUtfStringArray(Flag.LIST_NAMES, listUserNames);
        resObj.putIntArray(Flag.LIST_NUMBER_IN_Table, numberPlayerInRooms);
        resObj.putUtfStringArray(Flag.ROOM_NAME, listRoomNames);
        ext.send(Command.LIST_TABLE, resObj, user);
    }

    public void sendUpdateTable(String roomName, int numberPlayer, String groupName, User user) {
        Debug.d("send update table");
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.ROOM_NAME, roomName);
        resObj.putInt(PokerFlag.NUMBER_PLAYERS, numberPlayer);
        resObj.putUtfString(Flag.GROUP_NAME, groupName);
        ext.send(Command.UPDATE_TABLE, resObj, user);
        log(resObj);

    }
//      public void sendUpdateTable(SString roomName, int numberPlayer, String groupName, User user) {
//        Debug.d("send update table");
//        ISFSObject resObj = new SFSObject();
//        resObj.putUtfString(Flag.ROOM_NAME, roomName);
//        resObj.putInt(PokerFlag.NUMBER_PLAYERS, numberPlayer);
//        resObj.putUtfString(Flag.GROUP_NAME, groupName);
//        ext.send(Command.UPDATE_TABLE, resObj, user);
//    }

    void sendUpdateMoney(PokerPlayer pokerPlayer, List<Long> listMoneys) {
        Debug.d("send update money");
        ISFSObject resObj = new SFSObject();
        resObj.putLongArray(PokerFlag.LIST_ALL_MONEYS, listMoneys);
        resObj.putUtfString(PokerFlag.PLAYER_NAME, pokerPlayer.getName());
        resObj.putLong(PokerFlag.PLAYER_MONEY, pokerPlayer.getTotalMoney() + pokerPlayer.getMoney());
        resObj.putInt(PokerFlag.SEAT_POS, pokerPlayer.getSeatPos());
        ext.send(Command.UPDATE_MONEY, resObj, pokerPlayer.getUser());
    }

    private void log(ISFSObject resObj) {
        Debug.d(resObj.getDump());
    }
}
